function ProductSelector(){
    this.selectedItems = [];
    this.selectedValues = [];
    this.events = [];
}
ProductSelector.prototype.moneyMode;
ProductSelector.prototype.multiple;
ProductSelector.prototype.selectedItems;
ProductSelector.prototype.selectedItem;
ProductSelector.prototype.selectedValues;
ProductSelector.prototype.selectedValue;
ProductSelector.prototype.events;
ProductSelector.prototype.lastSelected;
ProductSelector.prototype.addEventListener = function(event,callback){
    this.events.push({event:event,callback:callback});
}
ProductSelector.prototype.dispatchEvent = function(event){
   this.events.forEach(function(e){
       if(e.event==event){
           e.callback.call(this);
       }
   });
}
ProductSelector.prototype.open = function(app_id){
    let firstLoad = this.selectedItems.length==0;
    let dom = this;
    let copy_selectedItems = JSON.parse(JSON.stringify(this.selectedItems));
    let copy_selectedItem = this.selectedItem?JSON.parse(JSON.stringify(this.selectedItem)):null;
    let copy_selectedValues = JSON.parse(JSON.stringify(this.selectedValues));
    let copy_selectedValue = this.selectedValue?this.selectedValue:null;
    $.mobile.loading( "show" );
    $.ajax({
        url:"/plugin/api/product.list?app_id="+app_id,
        method:"GET",
        success:function(res){
            let layer = $("<div></div>").css("width","100%").css("height","100%").css("position","fixed").css("font-size","12px").css("display","flex").css("justify-content","center").css("flex-wrap","wrap").css("background","rgba(255,255,255,1)").css("z-index","9999").css("overflow","auto").css("padding-top","60px").css("box-sizing","border-box");
            let cancel = $("<div>取消</div>").css("width","80px").css("height","100%").css("display","flex").css("justify-content","center").css("align-items","center").click(function(){
                dom.selectedItems = copy_selectedItems;
                dom.selectedItem = copy_selectedItem;
                dom.selectedValues = copy_selectedValues;
                dom.selectedValue = copy_selectedValue;
                dom.dispatchEvent("cancel");
                layer.remove();
            });
            let done = $("<div>选好了</div>").css("width","80px").css("height","100%").css("display","flex").css("justify-content","center").css("align-items","center").click(function(){
                dom.dispatchEvent("success");
                layer.remove();
            });
            let action = $("<div></div>").css("display","flex").append(cancel).append(done);
            layer.append($("<div></div>").css("position","fixed").css("width","100%").css("height","50px").css("background","#fff").css("border-bottom","#ccc solid 1px").css("top","0px").css("z-index","1").css("display","flex").css("justify-content","space-between").css("align-items","center").append($("<div>请选择商品</div>").css("padding-left","10px")).append(action));
            let products = res.result;
            products.forEach(function(product){
                let checkStyle = $("<img src=\"\">").addClass("checkStyle").css("position","absolute").css("right","0px").css("top","0px").css("width","20px").css("height","20px").hide();
                let box = $("<div></div>").css("position","relative").css("display","flex").css("flex-direction","column").css("justify-content","center").css("margin","5px");
                
                box.click(function(){
                    
                    if(dom.multiple){
                        if(!this.checked){
                            this.checked = true;
                            $(this.querySelector(".pic")).css("opacity",0.5);
                            dom.selectedItems.push(product);
                            dom.selectedValues.push(product.id);
                            checkStyle.show();
                        }else{
                            this.checked = false;
                            $(this.querySelector(".pic")).css("opacity",1);
                            let removeIndex = dom.selectedValues.indexOf(product.id);
                            dom.selectedValues.splice(removeIndex,1);
                            dom.selectedItems.splice(removeIndex,1);
                            checkStyle.hide();
                        }
                    }else{
                        if(dom.selectedItem==product){
                            return;
                        }
                        if(dom.lastSelected){
                            dom.lastSelected.checked = false;
                            $(dom.lastSelected.querySelector(".pic")).css("opacity",1);
                            $(dom.lastSelected.querySelector(".checkStyle")).hide();
                        }
                        this.checked = true;
                        $(this.querySelector(".pic")).css("opacity",0.5);
                        $(this.querySelector(".checkStyle")).show();
                    }
                    dom.selectedItem = this.checked?product:null;
                    dom.selectedValue = this.checked?product.id:null;
                    dom.lastSelected = this;
                    dom.dispatchEvent("change");
                });
                let pic = $("<img src=\""+product.album[0]+"?imageView2/5/w/80/h/80\"/>").addClass("pic").css("width","80px").css("height","80px").css("object-fit","cover");
                box.append(pic);
                box.append($("<span>"+product.name+"</span>").css("white-space","no-wrap").css("height","25px").css("line-height","25px").css("width","80px").css("overflow","hidden"));
                box.append($("<span>￥"+product[dom.moneyMode]+"</span>").css("position","absolute").css("font-size","10px").css("left","0px").css("top","0px").css("background","rgba(0,0,0,0.75)").css("padding","5px").css("color","#fff"));
                box.append(checkStyle);
                layer.append(box);
                if((dom.selectedValues&&dom.selectedValues.indexOf(product.id)!=-1)||(dom.selectedValue&&dom.selectedValue==product.id)){
                    box[0].checked = true;
                    pic.css("opacity",0.5);
                    checkStyle.show();
                    dom.selectedItem = product;
                    dom.lastSelected = box[0];
                    if(dom.multiple&&firstLoad){
                        dom.selectedItems.push(product);
                    }
                }
            });
            $(document.body).append(layer);
        },
        complete:function(){
            $.mobile.loading( "hide" );
        }
    });
}